<?php
namespace App\Logic\Tool;

use App\Models\Basics\SysArea;
use App\Models\Basics\SysCountry;
use App\Models\Basics\Transport;
use App\Models\Delivery\FBADelivery;
use App\Models\Delivery\FBADeliveryLogistics;
use App\Models\Inventory\InventoryCn;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductSku;
use App\Models\Shipping\SSku;
use App\Models\SsBasic\AreaSku;
use App\Models\SsBasic\AreaSkuAvg;
use App\Models\SsBasic\AreaSkuEstimate;
use App\Models\SsBasic\AreaSkuStock;
use App\Models\Stocking\StockingSetDate;
use App\Models\Stocking\StockingSku;
use App\Models\Stocking\StockingSkuRatio;
use Illuminate\Support\Facades\DB;
class ToolWork
{
    private static function _addSetDate($classifyId){
        $ps = ProductSku::query()
            ->where('status',1)
            ->where('classify_id',$classifyId)
            ->get();
        $pCycle = 0;
        foreach ($ps as $_ps){
            $pCycleTemp = ProductSku::getPuCycle($_ps);
            if($pCycle < $pCycleTemp){
                $pCycle = $pCycleTemp;
            }
        }
        $areas = SysArea::options();
        foreach ($areas as $areaId => $area){
            if(in_array($area,['OUT','PUBLIC','CN']))continue;
            $seaDay = Transport::getShipmentDay($areaId,1);
            StockingSetDate::addSetDate($areaId,$classifyId,$pCycle,$seaDay);
        }
    }

    private static function _logistics(){
        FBADelivery::query()
            ->where('logistics_status',2)
            ->chunk(500,function ($items){
                foreach ($items as $item){
                    $dl = FBADeliveryLogistics::query()
                        ->where('delivery_no',$item->delivery_no)
                        ->first();
                    if($dl){
                        if($dl->tracking_no){
                            $item->track_ex_status = 2;
                        }else{
                            $item->track_ex_status = 1;
                        }
//                        $item->save();
                    }
                }
        });
    }

    private static function _checkSSku(){
        SSku::query()
            ->chunk(500,function ($items){
                foreach ($items as $item){
                    $asCount = AreaSku::query()->where('area_psku',$item->area_psku)->count();
                    if($asCount == 0){
                        echo '1:'.$item->area_psku.PHP_EOL;
                        $countryIds = SysCountry::getCountryIdsByAreaId($item->area_id);
                        $pasCount = ProductAmazonSku::query()
                            ->whereIn('country_id', $countryIds)
                            ->where('psku_id', $item->psku_id)
                            ->where('status',1)
                            ->count();
                        if($pasCount == 0){
                            $item->delete();
                        }
                    }
                }
            });
    }

    private static function _inventoryArea(){
        $areaTempId = SysArea::getSysAreaIdByArea('TEMP'); //TEMP站点
        $areaPublicId = SysArea::getSysAreaIdByArea(); //PUBLIC站点
        $inventoryArea = InventoryCn::query()
            ->select('*',DB::raw('(instant+area_transit) AS totalInventory'))
            ->whereNotIn('area_id', [$areaPublicId,$areaTempId])
            ->where('psku_id', 1841)
            ->orderByDesc('totalInventory')
            ->lockForUpdate()
            ->first();
        if($inventoryArea){
            echo 11;
            dd($inventoryArea);
        }
        $inventoryArea->instant -= 1;
        $flag1 = $inventoryArea->save();
        echo $flag1;
        dd($inventoryArea);
    }

    private static function _checkStockSku(){
        StockingSku::query()
            ->chunk(500,function ($items){
                foreach ($items as $item){
                    $asCount = AreaSku::query()->where('area_psku',$item->area_psku)->count();
                    if($asCount == 0){
                        echo '1:'.$item->area_psku.PHP_EOL;
                        $countryIds = SysCountry::getCountryIdsByAreaId($item->area_id);
                        $pasCount = ProductAmazonSku::query()
                            ->whereIn('country_id', $countryIds)
                            ->where('psku_id', $item->psku_id)
                            ->where('status',1)
                            ->count();
                        if($pasCount == 0){
                            SSku::query()->where('area_psku',$item->area_psku)->delete();
                            StockingSkuRatio::query()->where('area_psku',$item->area_psku)->delete();
                            AreaSkuEstimate::query()->where('area_psku',$item->area_psku)->delete();
                            AreaSkuAvg::query()->where('area_psku',$item->area_psku)->delete();
                            AreaSkuStock::query()->where('area_psku',$item->area_psku)->delete();
                            $item->delete();
                        }
                    }
                }
            });
    }

    public static function work($func,$param)
    {
        ini_set('memory_limit', '1024M');
        set_time_limit(0);
        if($param){
            self::{'_'.$func}($param);
        }else{
            self::{'_'.$func}();
        }
    }
}
